package com.cgx.tablet.beeper.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.cgx.tablet.beeper.R;
import com.cgx.tablet.beeper.api.ApiUtil;
import com.cgx.tablet.beeper.base.BaseActivity;
import com.cgx.tablet.beeper.bean.WinTransBean;
import com.cgx.tablet.beeper.databinding.ActivityLoginBinding;
import com.cgx.tablet.beeper.dialog.IpDialog;
import com.cgx.tablet.beeper.event.FinishActivityEvent;
import com.cgx.tablet.beeper.presenter.LoginPresenter;
import com.cgx.tablet.beeper.utils.SharedPreferenceUtil;
import com.cgx.tablet.beeper.utils.ToastUtil;
import com.cgx.tablet.beeper.view.ILoginView;
import com.yatoooon.screenadaptation.ScreenAdapterTools;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class LoginActivity extends BaseActivity implements ILoginView, IpDialog.OnIPListener {

    private ActivityLoginBinding mBinding;
    private LoginPresenter loginPresenter;
    private IpDialog ipDialog;
    private ArrayAdapter<String> payTypeAdapter;
    private List<WinTransBean> winTransBeans = new ArrayList<>();
    private List<String> winNames = new ArrayList<>();
    private WinTransBean winTransBean;
    private int winIndex;
    private boolean ipSetting = false;
    public Timer timer = new Timer();
    private int times = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        ScreenAdapterTools.getInstance().reset(this);
        ScreenAdapterTools.getInstance().loadView(getWindow().getDecorView());
        mBinding.setLoginActivity(this);
        registerEventBus(FinishActivityEvent.TAG_EXITLOGIN);
        loginPresenter = new LoginPresenter(this);
        initSpinner();
        mBinding.loginUserEt.setText(SharedPreferenceUtil.getAccount(this));
        mBinding.loginPwdEt.setText(SharedPreferenceUtil.getPassword(this));
        initStatusBar(android.R.color.transparent);
        if (TextUtils.isEmpty(SharedPreferenceUtil.getIP(this))) {
            ipDialog = new IpDialog(this);
            ipDialog.setOnSaveEditListener(this);
            ipDialog.show();
        } else {
            ApiUtil.init(getString(R.string.service_domain, SharedPreferenceUtil.getIP(this), SharedPreferenceUtil.getPort(this)));
            loginPresenter.windowList();
        }
    }

    public void ipSetting(View view) {
        if (ipSetting) {
            ipDialog = new IpDialog(this);
            ipDialog.setOnSaveEditListener(this);
            ipDialog.show();
        } else {
            times ++;
            if (times == 2)
                ipSetting = true;
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    times = 0;
                    ipSetting = false;
                }
            }, 1000);
        }
    }

    public void login(View view) {
        loginPresenter.login(mBinding.loginUserEt.getText().toString(), mBinding.loginPwdEt.getText().toString(), winTransBean, winIndex);
    }

    @Override
    public void onGetWindowsSuccess(List<WinTransBean> winTransBeans) {
        this.winTransBeans.clear();
        winNames.clear();
        this.winTransBeans.addAll(winTransBeans);
        for (int i = 0; i < this.winTransBeans.size(); i++) {
            winNames.add(winTransBeans.get(i).getWinName());
        }
        payTypeAdapter.notifyDataSetChanged();
        mBinding.loginWindowSpinner.setSelection(SharedPreferenceUtil.getWindow(this));
    }

    @Override
    public void onGetWindowsFail(String code, String msg) {
        ToastUtil.showShort(this, msg);
    }

    public void initSpinner() {
        //TODO Spinner需要优化
        payTypeAdapter = new ArrayAdapter<>(this, R.layout.item_spinner, winNames);
        payTypeAdapter.setDropDownViewResource(R.layout.item_spinner_drop);
        mBinding.loginWindowSpinner.setDropDownWidth(280);
        mBinding.loginWindowSpinner.setDropDownVerticalOffset(mBinding.loginWindowSpinner.getMeasuredHeight());
        mBinding.loginWindowSpinner.setAdapter(payTypeAdapter);
        mBinding.loginWindowSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                winTransBean = winTransBeans.get(i);
                winIndex = i;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        ViewTreeObserver payTypeVto = mBinding.loginWindowSpinner.getViewTreeObserver();
        payTypeVto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mBinding.loginWindowSpinner.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                mBinding.loginWindowSpinner.setDropDownVerticalOffset(mBinding.loginWindowSpinner.getHeight());
            }
        });
    }

    @Override
    public void onLoginSuccess() {
        ToastUtil.showShort(this, R.string.login_success);
        finish();
        startActivity(MainActivity.class, "winName", winNames.get(winIndex));
    }

    @Override
    public void onLoginFail(String code, String msg) {
        ToastUtil.showShort(this, msg);
    }

    @Override
    public void exit() {
        ipDialog.dismiss();
        ipDialog = null;
        finish();
    }

    @Override
    public void save(String ip, String port) {
        times = 0;
        ipSetting = false;
        SharedPreferenceUtil.setIP(this, ip);
        SharedPreferenceUtil.setPort(this, port);
        ipDialog.dismiss();
        ipDialog = null;
        //初始化Retrofit和API接口
        ApiUtil.init(getString(R.string.service_domain, ip, port));
        loginPresenter.windowList();
    }

}
